package threecard.android.card;

import java.lang.reflect.Field;

import threecard.android.R;
import threecard.android.R.drawable;

import android.content.Context;
import android.util.Log;
import android.widget.ImageButton;

public class CustomImageButton extends ImageButton {

	private int mImageResource = 0;
	private int value = 0, suit = 0;
	private int imageId = 0; // ID of the card.

	public CustomImageButton(Context context, int value, int suit,
			Boolean faceUp) {
		super(context);
		this.value = value;
		this.suit = suit;

		// Get the card face's ID.
		try {
			Class<drawable> res = R.drawable.class;
			String name = (new PlayingCard(value, suit)).getPngName();

			Log.v("---", name);
			Field field = res.getField(name);
			imageId = field.getInt(null);
		} catch (Exception e) {
			Log.e("CustomImageButton", "Failure to get drawable id.", e);
		}

		if (!faceUp) {
			setImageResource(R.drawable.cardback);
		} else {
			setImageResource(imageId);
		}
		
		//Thin padding
		this.setPadding(0, 0, 0, 0);

	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public int getSuit() {
		return suit;
	}

	public void setSuit(int suit) {
		this.suit = suit;
	}

	@Override
	public void setImageResource(int resId) {
		mImageResource = resId;
		super.setImageResource(resId);
	}

	public int getImageResource() {
		return mImageResource;
	}

	//Flip the card.
	public void flip() {
		if (mImageResource == R.drawable.cardback){
			setImageResource(imageId);
		}else {
			setImageResource(R.drawable.cardback);
		}
			
	}

}
